<?php
namespace Wap\Controller;
 
class ShopController extends CommonController {
	/**
	 * 商家主页
	 */
	public function index() {
		$store_id = I('get.store_id');
		if($store_id) {
			$store = M($this -> tm_store) -> find($store_id);
			$product = M($this -> tm_product) -> where('st_id = ' . $store_id) -> limit(10) -> select();
			if($_SESSION['user_id']) {
				$collect = M('collect') -> where('userid = ' . $_SESSION['user_id'] . ' and sc_id = ' . $store_id . ' and sc_lx = "dp"') -> find();
			} else {
				$collect = '';
			}
			$this -> assign('collect', $collect);
			$this -> assign('store', $store);
			$this -> assign('product', $product);
		} else {
			$this -> error('非法操作');
		}
		$this -> display();
	}

	/**
	 * 购物车
	 */
	public function cart() {
		$this -> isLogin();
		$shop_cart = M('shop_cart') -> where('buyer_id = %s', $_SESSION['user_id']) -> select();
		$shop_cart_data = array();
		foreach($shop_cart as $k => $v) {
			$shop_cart_data[$v['store_id']]['store_name'] = $v['store_name'];
			$shop_cart_data[$v['store_id']]['item'][] = $v;
		}
		$this -> assign('shop_cart_data', $shop_cart_data);
		$this -> display();
	}
	
	/**
	 * 添加购物车
	 */
	public function add_cart() {
		if(!isset($_SESSION['boboid']) && !isset($_SESSION['user_id'])) {
			echo json_encode(array('code' => 2));
			exit;
		}
		if(IS_POST) {
			$post = I('post.');
			$goods_id = $post['goods_id'];
			$product = M($this -> tm_product) -> find($goods_id);
			$store = M($this -> tm_store) -> find($product['st_id']);
			if($store['member_id'] == $_SESSION['user_id']) {
				echo json_encode(array('code' => 1, 'message' => '不能购买自己店铺.'));
				exit;
			}
			$shop_cart = M('shop_cart');
			$shop_cart_data = $shop_cart -> where('goods_id = %d and buyer_id = %d and color_n = "%s" and spec_n = "%s"', $goods_id, $_SESSION['user_id'], $post['color_n'], $post['spec_n']) -> find();
			if($shop_cart_data) {
				$data['cart_id'] = $shop_cart_data['cart_id'];
				$data['goods_num'] = $shop_cart_data['goods_num'] + 1;
				if($shop_cart -> save($data)) {
					echo json_encode(array('code' => 0, 'message' => '加入购物车成功.'));
				} else {
					echo json_encode(array('code' => 1, 'message' => '加入购物车失败.'));
				}
				exit;
			}
			$area_ids = unserialize($product['shop_area_ids']);
			$payment = M('shop_area') -> join('tm_payment ON tm_payment.payment_id = tm_shop_area.pay_id') -> field('tm_payment.payment_code') -> find($area_ids[0]);
			$data['buyer_id'] = $_SESSION['user_id'];
			$data['store_id'] = $product['st_id'];
			$data['store_name'] = $store['store_name'];
			$data['goods_id'] = $product['id'];
			$data['goods_name'] = $product['productname'];
			$data['goods_price'] = $post['price'];
			$data['goods_num'] = 1;
			$data['goods_image'] = $product['pic'];
			$data['bl_id'] = 0;
			$data['point_num'] = $post['point_num'];
			$data['payment_code'] = $payment['payment_code'];
			$data['coin_num'] = $product['coin_num']?$product['coin_num']:0;
			$data['give_coin'] = $product['give_coin']?$product['give_coin']:0;
			$data['color_n'] = $post['color_n'];
			$data['spec_n'] = $post['spec_n'];
			if($shop_cart -> add($data)) {
				echo json_encode(array('code' => 0, 'message' => '加入购物车成功.'));
			} else {
				echo json_encode(array('code' => 1, 'message' => '加入购物车失败.'));
			};
			exit;
		}
	}
	
	public function delete_cart() {
		if(!isset($_SESSION['boboid']) && !isset($_SESSION['user_id'])) {
			echo json_encode(array('code' => 2));
			exit;
		}
		if(IS_POST) {
			$data = I('post.');
			$cart_ids = '';
			foreach($data['cart_ids'] as $v) {
				$cart_ids .= $v . ',';
			}
			$cart_ids = substr($cart_ids, 0, strlen($cart_ids) - 1);
			if(M('shop_cart') -> delete($cart_ids)) {
				echo json_encode(array('code' => 0, 'message' => '删除成功.'));
			} else {
				echo json_encode(array('code' => 1, 'message' => '删除失败.'));
			};
			exit;
		}
	}
}